package logic

import (
	"context"
	"fmt"
	"shopping/model/mysql"
	redis2 "shopping/model/redis"
	"strconv"

	"shopping/shoprpc/internal/svc"
	"shopping/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LikeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikeLogic {
	return &LikeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LikeLogic) Like(in *shoprpc.LikeRequest) (*shoprpc.LikeResponse, error) {
	// todo: add your logic here and delete this line
	var like mysql.Good
	mysql.DB.Debug().Where("id = ?", in.Id).Find(&like)

	if like.ID == 0 {
		return nil, fmt.Errorf("当前商品不存在")
	}
	if like.UserId != int(in.UserId) {
		return nil, fmt.Errorf("当前商品不是该用户的")
	}

	itoa := strconv.Itoa(int(in.UserId))
	redis2.Rdb.ZIncrBy("likelist"+itoa, 1, like.Name)
	return &shoprpc.LikeResponse{}, nil
}
